package com.eoner.baselibrary.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetCheckUtil {

    public static boolean checkNet(Context context) {
        // 判断是否具有可以用于通信渠道
        boolean mobileConnection = isMobileConnection(context);
        boolean wifiConnection = isWIFIConnection(context);
        // 没有网络
        return mobileConnection || wifiConnection;
    }

    /**
     * 判断手机接入点（APN）是否处于可以使用的状态
     */
    public static boolean isMobileConnection(Context context) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo;
            if (manager != null) {
                networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                return networkInfo != null && networkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断当前wifi是否是处于可以使用状态
     */
    public static boolean isWIFIConnection(Context context) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo;
            if (manager != null) {
                networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                return networkInfo != null && networkInfo.isAvailable();
            }
        }
        return false;
    }
}
